﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Web;

namespace MyWebTest
{
    #region 需要下载文件实体类

    /// <summary>
    /// 需要下载文件实体类
    /// </summary>
    public class SynFileInfo
    {
        public string DocID { get; set; }
        public string DocName { get; set; }
        public long FileSize { get; set; }
        public string SynSpeed { get; set; }
        public string SynProgress { get; set; }
        public string DownPath { get; set; }
        public string SavePath { get; set; }
        public bool Async { get; set; }
        public DateTime LastTime { get; set; }
        public SynFileInfo()
        {
        }
        public SynFileInfo(object[] objectArr)
        {
            int i = 0;
            DocID = objectArr[i].ToString(); i++;
            DocName = objectArr[i].ToString(); i++;
            FileSize = Convert.ToInt64(objectArr[i]); i++;
            SynSpeed = objectArr[i].ToString(); i++;
            SynProgress = objectArr[i].ToString(); i++;
            DownPath = objectArr[i].ToString(); i++;
            SavePath = objectArr[i].ToString(); i++;
            Async = Convert.ToBoolean(objectArr[i]); i++;
        }
    }
    #endregion

    #region 文件相关操作类分

    /// <summary>
    /// 文件有关的操作类
    /// </summary>
    public class FileOperate
    {
        #region 相应单位转换常量

        private const double KBCount = 1024;
        private const double MBCount = KBCount * 1024;
        private const double GBCount = MBCount * 1024;
        private const double TBCount = GBCount * 1024;

        #endregion

        #region 获取适应大小

        /// <summary>
        /// 得到适应大小
        /// </summary>
        /// <param name="size">字节大小</param>
        /// <param name="roundCount">保留小数(位)</param>
        /// <returns></returns>
        public static string GetAutoSizeString(double size, int roundCount)
        {
            if (KBCount > size) return Math.Round(size, roundCount) + "B";
            else if (MBCount > size) return Math.Round(size / KBCount, roundCount) + "KB";
            else if (GBCount > size) return Math.Round(size / MBCount, roundCount) + "MB";
            else if (TBCount > size) return Math.Round(size / GBCount, roundCount) + "GB";
            else return Math.Round(size / TBCount, roundCount) + "TB";
        }

        #endregion
    }

    #endregion

    /// <summary>
    /// 下载
    /// </summary>
    public class DownloadTest
    {
        #region 检查网络状态

        //检测网络状态
        [DllImport("wininet.dll")]
        extern static bool InternetGetConnectedState(out int connectionDescription, int reservedValue);
        /// <summary>
        /// 检测网络状态
        /// </summary>
        public static bool IsConnected()
        {
            int I = 0;
            bool state = InternetGetConnectedState(out I, 0);
            return state;
        }

        #endregion
    }
}